angular.module('weixinpay.controller', []).controller('weixinpayController', ['$scope', '$http', '$location', function ($scope , $http, $location) {

    console.log('JSON object :' + JSON.stringify($location.search()));
    $scope.queryParams = $location.search();

    $scope.total_fee = parseFloat($scope.queryParams.total_fee);
    $scope.out_trade_no = $scope.queryParams.out_trade_no;

    jQuery('#qrcode').qrcode({width: 200, height: 200, text: $location.search().code_url});

    $scope.checkConfig = {
        setIntervalVal: null,
        checkTimes: 0,
        maxCheckTimes: 20,
        perTime: 5000
    };
    // $scope.checkConfig.setIntervalVal = setInterval($scope.checkTradeNo, $scope.checkConfig.perTime);
    $scope.checkConfig.setIntervalVal = setInterval(function () {
        $scope.checkTradeNo();
    }, $scope.checkConfig.perTime);

    $scope.checkTradeNo = function () {
        $scope.checkConfig.checkTimes ++;
        if ($scope.checkConfig.checkTimes > $scope.checkConfig.maxCheckTimes) {
            clearInterval($scope.checkConfig.setIntervalVal);
            jQuery('.donateFailAlert1').css('display','block');
            jQuery('.donateFailAlert2').css('display','block');
            return;
        }

        $http({
            method: 'GET',
            url: '/pay/wx/ordersStatus/' +  $scope.out_trade_no,
        }).success(function (resultData) {
            if(resultData && resultData.code == 0){
                var payStatus = resultData.data;
                // payStatus == 0 为初始状态
                if(payStatus == -1){
                    clearInterval($scope.checkConfig.setIntervalVal);
                    jQuery('.donateFailAlert1').css('display','block');
                    jQuery('.donateFailAlert2').css('display','block');
                }
                else if(payStatus == 1){
                    
                    clearInterval($scope.checkConfig.setIntervalVal);
                    $scope.paySuccess();
                }
                else {
                    console.log(JSON.stringify(resultData));
                }

   /*             //TODO 测试
                clearInterval($scope.checkConfig.setIntervalVal);
                $location.url('/paynotify?'+'');*/
            }
        }).error(function (error) {
            console.log('error: ' + JSON.stringify(error));
        });
    };
    $scope.$on("$destroy",function (event) {
        // 页面销毁时,取消定时查询任务
        console.log('clearInterval when destroy');
        clearInterval($scope.checkConfig.setIntervalVal);
    });

    $scope.hideDialog = function () {
        jQuery('.donateFailAlert1').css('display','none');
        jQuery('.donateFailAlert2').css('display','none');

        $scope.checkConfig.checkTimes = 0;
        $scope.checkConfig.setIntervalVal = setInterval(function () {
            $scope.checkTradeNo();
        }, $scope.checkConfig.perTime);
    }
    $scope.paySuccess = function () {
        var nextQueryParams = '';
        for (var prop in $scope.queryParams) {
            nextQueryParams += '&' + prop + '=' + $scope.queryParams[prop];
        }
        $location.url('/paynotify?' + nextQueryParams.substr(1));
    }

}]);